package net.pws.common.persistence;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class EntityManagerFactory implements
                                 InitializingBean,
                                 ApplicationContextAware {
    
    public static EntityManager getEntityManager() {
        return entityManager;
    }
    
    private static EntityManager entityManager;
    
    private ApplicationContext applicationContext;
    
    private String entityManagerName = "entityManager";
    
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    
    public void setEntityManagerName(String entityManagerName) {
        this.entityManagerName = entityManagerName;
    }
    
    public void afterPropertiesSet() throws Exception {
        entityManager = (EntityManager) getApplicationContext().getBean(entityManagerName);
    }
    
}
